import vue from 'vue'
import vuex from 'vuex'
import api from '@/api'
import {Loading} from 'element-ui'

vue.use(vuex)
export default new vuex.Store({
        state: {
            user: {
                uid: '', //用户id
                petname: '', //昵称
                name: '', //姓名
                sex: '', //性别
                hard: '', //头像
                role: '', //角色
            },
        },
        mutations: {
            user: (state, value) => {
                state.user = value;
            }
        },
        actions: {
            /*用户信息*/
            loadUserInfo({commit}) {
                const loading = Loading.service({text: '正在加载用户信息'});
                api.getUserInfo().then(({data: resp}) => {
                    if (resp.code === 200) {
                        commit('user', {
                            uid: resp.data.uid,
                            petname: resp.data.petname,
                            name: resp.data.name,
                            sex: resp.data.sex,
                            hard: resp.data.hard,
                            role: resp.data.role,
                        });
                        sessionStorage.name=resp.data.name
                        sessionStorage.petname=resp.data.petname
                    }
                    loading.close();
                }).catch(() => {
                    loading.close();
                })
            },
        }
    }
)
